#!/usr/bin/perl -w
   
use strict;
   
use SOAP::Lite;
use HoSTC_Service;
use UNIVERSAL::isa;

my $live_total_mkt = HoSTC_Service->GetLiveTotalMKT();
my $live_security_dataset = HoSTC_Service->GetLiveSecurityDATASET();
my $live_security = HoSTC_Service->GetLiveSecurity(1);
my $live_security_status = HoSTC_Service->LiveSecurityStatus();

my @stocks ;
my @schemas;

my %total_mkt_format = (
	cur_index => 0,					# the current index
	prv_index => 0,					# the previous index
	chg_amount => 0,				# the changed amount
	num_transactions => 0,			# the total number of transactions
	num_exchanged_stocks => 0,		# the total number of exchanged stocks
	tot_money_amount => 0,			# the total amount of money 
	upd_date => '',					# the update date/time
	cls_session => 'N',				# is the session closed?
	ses_id => 0						# the session's id
);

# get the live total maket data
#
for my $t ($live_total_mkt->valueof('//GetLiveTotalMKTResponse')) {
    print $t->{'GetLiveTotalMKTResult'}. "\n";
}

# get the live security data set
#
if ($live_security_dataset->fault) {	# if not succeed
      print $live_security_dataset->faultdetail;
} else {
	# declare the path to each information piece that will be extracted
	my $rot_xpath = '//GetLiveSecurityDATASETResponse/GetLiveSecurityDATASETResult'; # root path
	my $dat_xpath = $rot_xpath . '/diffgram/NewDataSet/Table'; # the relative path to the stock data
	my $sch_xpath = $rot_xpath . '/schema/element/complexType/choice/element/complexType/sequence/element'; # the relative path to the schema data 
	# get the schema elements
	my $count = 0;	
	foreach my $sch_tmp ($live_security_dataset->dataof($sch_xpath)){
		my $schema = {} ;
		$schema->{'name'} = $sch_tmp->attr->{'name'};
		$schema->{'OraDbType'} = $sch_tmp->attr->{'OraDbType'};
		$schema->{'type'} = $sch_tmp->attr->{'type'};
		push @schemas, $schema;
		#print $schemas[$count]->{"name"};
		$count ++;
	}
	# get live stock data and push to a hash (%stock) 
	# then push the hash to an array (@stocks)    	
	foreach my $sch_dat ($live_security_dataset->valueof($dat_xpath)){
		my $stock = {} ;
		foreach (@schemas){
			$stock->{$_->{'name'}} = $sch_dat->{$_->{'name'}};
			#print $_->{"name"} . "=" . $stock->{$_->{'name'}} . " ";			  
		}	
		push @stocks, $stock;
		#print "\n";	
	}
}
# get live security
if ($live_security->fault) {	# if not succeed
      print $live_security->faultdetail;
}else{
	print "live security \n";
	for my $t ($live_security->valueof('//GetLiveSecurityResponse')) {
	    print $t->{'GetLiveSecurityResult'} . "\n";
	}
}

# get live security
if ($live_security_status->fault) {	# if not succeed
    print $live_security_status->faultdetail;
}else{
	print "live security status \n";
	for my $t ($live_security_status->valueof('//LiveSecurityStatusResponse')) {
	    print $t->{'LiveSecurityStatusResult'} . "\n";
	}
}
 
exit;